package com.boc.langxiang.presenter.shalo;


import com.boc.langxiang.contract.shalo.ShaloDetailContract;
import com.boc.langxiang.entity.request.AddToShoppingCarRequest;
import com.boc.langxiang.entity.request.ShaLoListRequest;
import com.boc.langxiang.entity.request.ShaloBaomingRequest;
import com.boc.langxiang.entity.request.ShaloMessageRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.ShaloBaomingContent;
import com.boc.langxiang.entity.response.ShaloDetailContent;
import com.boc.langxiang.entity.response.ShaloDetailEntity;
import com.boc.langxiang.entity.response.ShaloListContent;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class ShaloDetailPresenter implements ShaloDetailContract.Presenter {

    private final ApiService mService;
    private ShaloDetailContract.View mView;

    public ShaloDetailPresenter(ShaloDetailContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }


    @Override
    public void shaloMessage(int id, String content) {
        mView.showLoading();
        ShaloMessageRequest request = new ShaloMessageRequest();
        request.id = id;
        request.content = content;
        mService.shaloMessage(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.shaloMessageSuccess();
                    }
                });
    }

    @Override
    public void addToShoppingCar(AddToShoppingCarRequest request) {
        mView.showAddShopCarLoading();
        mService.addToShoppingCar(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.addToShoppingCarSuccess();
                    }
                });
    }

    @Override
    public void shaDetail(int id) {
//        mView.showLoading();
        mService.shaDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShaloDetailContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShaloDetailContent> response) {
                        ShaloDetailContent content = response.getContent();
                        if (content != null) {
                            ShaloDetailEntity shaloDetailEntity = content.getRes();
                            mView.shaDetailSuccess(shaloDetailEntity);
                        }
                    }
                });
    }

    @Override
    public void shaloBaoming(ShaloBaomingRequest request) {
        mView.showBaomingLoading();
        mService.shaloBaoming(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShaloBaomingContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShaloBaomingContent> response) {
                        ShaloBaomingContent content = response.getContent();
                            mView.BaomingSuccess(content);
                    }
                });
    }
}
